
function readHtml(res,html){
    res.writeHead(200,{'content-type':'text/html;charset=utf8'})
    res.write(html)
    res.end()
}

const {readHtmlAsync,readJsonAsync,readImgAsync} = require('../files')
const url = require('url')
const fs = require('fs')

module.exports = {
    views(req,res){
        const pathname = url.parse(req.url).pathname.replace(/\//,'')
        fs.stat(pathname,(err,stat)=>{
            if(err){
                readHtmlAsync(res,'views/notfound.html')
            }else{
                //1.GET请求
                // var query = url.parse(req,url,true).query
                // console.log(query)
                readHtmlAsync(res,pathname);
            }
        })
    },
    // home(req,res){
    //     readHtmlAsync(res,'views/home.html')
    // },
    // mine(req,res){
    //     readHtmlAsync(res,'views/mine.html')
    // },
    // login(req,res){
    //     readHtmlAsync(res,'views/login.html')
    // },
    // zhuce(req,res){
    //     readHtmlAsync(res,'views/zhuce.html')
    // },
    // notfound(req,res){
    //     readHtmlAsync(res,'views/notfound.html')
    // },
    json(req,res){
        readJsonAsync(res,'data/data.json')
    },
    // photo(req,res){
    //     readImgAsync(res,'imgs/img1.jpeg')
    // },
    imgs(req,res){
        const pathname = url.parse(req.url).pathname.replace(/\//,'')

        //判断这个路径有没有该文件
        //404判断
        fs.stat(pathname,(err,stat)=>{
            if(err){
                readHtmlAsync(res,'views/notfound.html')
            }else{
                readImgAsync(res,pathname);
            }
        })
    },
}